﻿namespace CpuUsageAPI
{
    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;

    internal class ProcessCPU
    {
        public const uint PROCESS_ALL_ACCESS = 0x1f0fff;
        public const int PROCESS_ENTRY_32_SIZE = 0x128;
        public static readonly IntPtr PROCESS_HANDLE_ERROR = new IntPtr(-1);
        public static readonly IntPtr PROCESS_LIST_ERROR = new IntPtr(-1);
        public const uint TH32CS_SNAPPROCESS = 2;

        [DllImport("KERNEL32.DLL")]
        public static extern bool CloseHandle(IntPtr Handle);
        [DllImport("kernel32.dll", SetLastError=true)]
        public static extern IntPtr CreateToolhelp32Snapshot(uint Flags, uint ProcessID);
        [DllImport("kernel32.dll")]
        public static extern bool GetProcessTimes(IntPtr ProcessHandle, out System.Runtime.InteropServices.ComTypes.FILETIME CreationTime, out System.Runtime.InteropServices.ComTypes.FILETIME ExitTime, out System.Runtime.InteropServices.ComTypes.FILETIME KernelTime, out System.Runtime.InteropServices.ComTypes.FILETIME UserTime);
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(uint DesiredAccess, bool InheritHandle, uint ProcessId);
        [DllImport("KERNEL32.DLL")]
        public static extern bool Process32First(IntPtr Handle, ref ProcessEntry32 ProcessInfo);
        [DllImport("KERNEL32.DLL")]
        public static extern bool Process32Next(IntPtr Handle, ref ProcessEntry32 ProcessInfo);
    }
}

